package com.loyal.loyalerp.humenresource.employee.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loyal.loyalerp.basedata.company.mapper.GyCompanyMapper;
import com.loyal.loyalerp.basedata.department.mapper.DepartmentMapper;
import com.loyal.loyalerp.basedata.department.model.Department;
//import com.loyal.loyalerp.humenresource.department.model.Department;
import com.loyal.loyalerp.humenresource.employee.data.Gy_PersonData;
import com.loyal.loyalerp.humenresource.employee.entity.Gy_Person;
import com.loyal.loyalerp.humenresource.employee.mapper.Gy_PersonMapper;
import com.loyal.loyalerp.humenresource.employee.service.Gy_PersonReadService;
@Service
public class Gy_PersonReadServiceImpl implements Gy_PersonReadService{
	
	@Autowired
	private Gy_PersonMapper gy_PersonMapper;
	
	@Autowired
	private DepartmentMapper deptMapper;
	
	@Autowired
	private GyCompanyMapper companyMapper;

	//列出所有的员工信息
	public List<Gy_PersonData> retrieveAllPerson() {
		return gy_PersonMapper.selectAllPerson();
		 
		
	}

	@Override
	public Gy_Person retrievePersonByPrimaryKey(Long id) {
		return gy_PersonMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Map<String, Object>> selectCompanyNameWithId(Department record) {
		return gy_PersonMapper.selectCompanyNameWithId(record);
	}


	@Override
	public List<Gy_Person> listPerson() {
		return gy_PersonMapper.listPerson();
	}


	@Override
	public List<Gy_Person> selectByPCD(String active,String pname, String cname, String dname) {
		
		Byte activeByte=0;
		Integer companyid=null;
		Department dept=null;
		
		if("0".equals(active)) {
			activeByte=null;
		}else if("1".equals(active)) {
			activeByte=1;
		}else if("2".equals(active)) {
			activeByte=0;
		}
		if(cname==null || "".equals(cname) ) {
			
		}else {
			companyid=companyMapper.selectCompanyIDByName(cname);
		}
		System.out.println(activeByte);
		System.out.println(pname);
		System.out.println(companyid);
		if(dname==null || "".equals(dname)) {
			return gy_PersonMapper.selectByPCD(activeByte,pname, companyid, null);
		}else {
			dept=deptMapper.selectByDeptname(dname);
		}
		
		return gy_PersonMapper.selectByPCD(activeByte,pname, companyid, dept.getId());

	}

	@Override
	public List<Gy_Person> selectByActive(String active) {
		if("0".equals(active)) {
			return gy_PersonMapper.listPerson();
		}else if("1".equals(active)) {
			return gy_PersonMapper.selectByActive((byte)1);
		}else if("2".equals(active)) {
			return gy_PersonMapper.selectByActive((byte)0);
		}
		return null;
	}

	@Override
	public String exportExcel(String isActive, HttpServletResponse response) throws FileNotFoundException {
		XSSFWorkbook book = new XSSFWorkbook();
		XSSFSheet sheet = book.createSheet();
		XSSFRow row = sheet.createRow(0);
		row.createCell(0).setCellValue("ID");
		row.createCell(1).setCellValue("公司名称");
		row.createCell(2).setCellValue("部门名称");
		row.createCell(3).setCellValue("员工编码");
		row.createCell(4).setCellValue("员工姓名");
		row.createCell(5).setCellValue("员工英文姓名");
		row.createCell(6).setCellValue("性别");
		row.createCell(7).setCellValue("名族");
		row.createCell(8).setCellValue("婚姻状况");
		row.createCell(9).setCellValue("职位");
		row.createCell(10).setCellValue("身份证号");
		row.createCell(11).setCellValue("身份证地址");
		row.createCell(12).setCellValue("现居住地");
		row.createCell(13).setCellValue("出生日期");
		row.createCell(14).setCellValue("毕业日期");
		row.createCell(15).setCellValue("入职日期");
		row.createCell(16).setCellValue("转正日期");
		row.createCell(17).setCellValue("是否转正");
		row.createCell(18).setCellValue("劳动合同到期日");
		row.createCell(19).setCellValue("是否签劳动合同");
		row.createCell(20).setCellValue("离职日期");
		row.createCell(21).setCellValue("学历");
		row.createCell(22).setCellValue("毕业院校");
		row.createCell(23).setCellValue("专业");
		row.createCell(24).setCellValue("英语等级");
		row.createCell(25).setCellValue("银行卡号");
		row.createCell(26).setCellValue("公积金");
		row.createCell(27).setCellValue("社保卡号");
		row.createCell(28).setCellValue("金融社保卡号");
		row.createCell(29).setCellValue("工卡");
		row.createCell(30).setCellValue("港澳通行证号");
		row.createCell(31).setCellValue("紧急联系人");
		row.createCell(32).setCellValue("紧急联系电话");
		row.createCell(33).setCellValue("邮箱");
		row.createCell(34).setCellValue("手机");
		row.createCell(35).setCellValue("微信号");
		row.createCell(36).setCellValue("qq号");
		row.createCell(37).setCellValue("备注");
		row.createCell(38).setCellValue("创建时间");
		row.createCell(39).setCellValue("修改时间");
		row.createCell(40).setCellValue("是否在职");
		List<Gy_Person> personList=null;
		if("0".equals(isActive)) {
			personList=gy_PersonMapper.listPerson();
		}else if("1".equals(isActive)) {
			personList=gy_PersonMapper.selectByActive((byte)1);
		}else if("2".equals(isActive)) {
			personList=gy_PersonMapper.selectByActive((byte)0);
		}
		for (int i = 0; i < personList.size(); i++) {
			Gy_Person person = personList.get(i);
			System.out.println(person);
			XSSFRow newRow = sheet.createRow(i + 1);
			newRow.createCell(0).setCellValue(person.getId()==null ? "" : person.getId()+"");
			
			String companyname="";
			if(person.getCompanyid()!=null) {
				companyname=companyMapper.selectByPrimaryKey(person.getCompanyid()).getCompanyname()+"";
			}
			newRow.createCell(1).setCellValue(companyname);
			
			String deptname="";
			if(person.getDeptid()!=null) {
				deptname=deptMapper.selectByPrimaryKey(person.getDeptid()).getDeptName()+"";
			}
			newRow.createCell(2).setCellValue(deptname);
			
			newRow.createCell(3).setCellValue(person.getPersoncode()==null ? "" : person.getPersoncode());
			newRow.createCell(4).setCellValue(person.getPersonname()==null ? "" : person.getPersonname());
			newRow.createCell(5).setCellValue(person.getPersonename()==null ? "" : person.getPersonename());
			String sex="";
			if(person.getSexid()==1000) {
				sex="男";
			}else if(person.getSexid()==1001) {
				sex="女";
			}
			newRow.createCell(6).setCellValue(sex);
			
			String nation="";
			if(person.getNationid()==null) {
				
			}else {
				if(person.getNationid()==1002) {
					nation="汉族";
				}else if(person.getNationid()==1003) {
					nation="非汉族";
				}
			}
			newRow.createCell(7).setCellValue(nation);
			
			String marrage="";
			if(person.getMarriageid()==null) {
				
			}else {
				if(person.getMarriageid()==1060) {
					marrage="未婚";
				}else if(person.getMarriageid()==1061) {
					marrage="已婚";
				}else if(person.getMarriageid()==1062) {
					marrage="脚踏两只船";
				}
			}
			newRow.createCell(8).setCellValue(marrage);
			
			String post="";
			if(person.getPostid()==null) {
			}else {
				if(person.getPostid()==1065) {
					post=("董事长");
				}else if(person.getPostid()==1066) {
					post=("执行总裁");
				}else if(person.getPostid()==1067) {
					post=("人事经理");
				}else if(person.getPostid()==1068) {
					post=("财务经理");
				}else if(person.getPostid()==1069) {
					post=("内务经理");
				}else if(person.getPostid()==1070) {
					post=("项目经理");
				}else if(person.getPostid()==1071) {
					post=("研发部经理");
				}else if(person.getPostid()==1072) {
					post=("人事助理");
				}else if(person.getPostid()==1073) {
					post=("文员");
				}else if(person.getPostid()==1074) {
					post=("销售经理");
				}else if(person.getPostid()==1075) {
					post=("销售人员");
				}else if(person.getPostid()==1076) {
					post=("采购经理");
				}else if(person.getPostid()==1077) {
					post=("采购文员");
				}
			}
			newRow.createCell(9).setCellValue(post);
			
			newRow.createCell(10).setCellValue(person.getIdentityno()==null ? "" : person.getIdentityno());
			newRow.createCell(11).setCellValue(person.getHomeaddr()==null ? "" : person.getHomeaddr());
			newRow.createCell(12).setCellValue(person.getNowaddr()==null ? "" : person.getNowaddr());
			newRow.createCell(13).setCellValue(person.getBirthdate()==null ? "" : person.getBirthdate()+"");
			newRow.createCell(14).setCellValue(person.getGraduatedate()==null ? "" : person.getGraduatedate()+"");
			newRow.createCell(15).setCellValue(person.getOfficedate()==null ? "" : person.getOfficedate()+"");
			newRow.createCell(16).setCellValue(person.getMemberdate()==null ? "" : person.getMemberdate()+"");
			newRow.createCell(17).setCellValue(person.getIsmembered()==null ? "" : person.getIsmembered()+"");
			newRow.createCell(18).setCellValue(person.getContractdate()==null ? "" : person.getContractdate()+"");
			newRow.createCell(19).setCellValue(person.getIscontracted()==null ? "" : person.getIscontracted()+"");
			newRow.createCell(20).setCellValue(person.getLeavedate()==null ? "" : person.getLeavedate()+"");
			String educationDegree="";
			if(person.getEducationdegree()==null) {
			}else {
				if(person.getEducationdegree()==1078) {
					educationDegree="硕士";
				}else if(person.getEducationdegree()==1079) {
					educationDegree="本科";
				}
			}
			newRow.createCell(21).setCellValue(educationDegree);
			
			newRow.createCell(22).setCellValue(person.getSchool()==null ? "" : person.getSchool());
			newRow.createCell(23).setCellValue(person.getMajor()==null ? "" : person.getMajor());
			String englishDegree="";
			if(person.getEnglishdegreeid()==null) {
			}else {
				if(person.getEnglishdegreeid()==1080) {
					englishDegree="硕士";
				}else if(person.getEnglishdegreeid()==1081) {
					englishDegree="本科";
				}
			}
			newRow.createCell(24).setCellValue(englishDegree);
			newRow.createCell(25).setCellValue(person.getBankCardNo()==null ? "" : person.getBankCardNo());
			newRow.createCell(26).setCellValue(person.getProvidentFund()==null ? "" : person.getProvidentFund());
			newRow.createCell(27).setCellValue(person.getInsuranceno()==null ? "" : person.getInsuranceno());
			newRow.createCell(28).setCellValue(person.getBankinsuranceno()==null ? "" : person.getBankinsuranceno());
			newRow.createCell(29).setCellValue(person.getCompanycardno()==null ? "" : person.getCompanycardno());
			newRow.createCell(30).setCellValue(person.getHkmcpassport()==null ? "" : person.getHkmcpassport());
			newRow.createCell(31).setCellValue(person.getIceperson()==null ? "" : person.getIceperson());
			newRow.createCell(32).setCellValue(person.getIcetel()==null ? "" : person.getIcetel());
			newRow.createCell(33).setCellValue(person.getEmail()==null ? "" : person.getEmail());
			newRow.createCell(34).setCellValue(person.getMobile()==null ? "" : person.getMobile());
			newRow.createCell(35).setCellValue(person.getWeixin()==null ? "" : person.getWeixin());
			newRow.createCell(36).setCellValue(person.getQq()==null ? "" : person.getQq());
			newRow.createCell(37).setCellValue(person.getRemark()==null ? "" : person.getRemark());
			newRow.createCell(38).setCellValue(person.getCreatetime()==null ? "" : person.getCreatetime()+"");
			newRow.createCell(39).setCellValue(person.getUpdatetime()==null ? "" : person.getUpdatetime()+"");
			String activeStr="";
			if(person.getActive()==null) {
			}else {
				if(person.getActive()==0) {
					activeStr="离职";
				}else if(person.getActive()==1) {
					activeStr="在职";
				}
			}
			newRow.createCell(40).setCellValue(activeStr);
		}
		File file = new File("员工表.xlsx");
		FileOutputStream fos = new FileOutputStream(file);
		try {
			book.write(fos);
			response.setHeader("Content-disposition",
					"attachment; fileName=" + new String("员工表.xlsx".getBytes("gbk"), "iso8859-1"));
			response.setContentType("application/x-rarx-rar-compressed");
			OutputStream out = response.getOutputStream();
			FileInputStream inputStream = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = inputStream.read(buffer)) != -1) {
				out.write(buffer, 0, i);
			}
			out.flush();
			out.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
